package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.OrderService;
import constant.MessageConstant;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Order;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * 体检预约
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Reference
    private OrderService orderService;
    
    @RequestMapping("/submit")
    public Result submitOrder(@RequestBody Map map,@RequestParam("trueCode") String trueCode){
        String inputCode = (String) map.get("validateCode");
        if(trueCode == null || !trueCode.equals(inputCode)){
            //验证码输入错误
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //验证码正确，进行预约服务
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        Result result = null;
        try{
            result = orderService.order(map);
        }catch (Exception e){
            System.out.println("this is OrderController Exception");
            e.printStackTrace();
        }
        return result;
    }
    
    @RequestMapping("/findOrderWithMemberAndSetmeal")
    public Result findOrderWithMemberAndSetmeal(Integer orderId){
        //查询的预约信息除了预约信息也包含会员信息和套餐信息
        try{
            Map map = orderService.findOrderWithMemberAndSetmeal(orderId);
            System.out.println(map.get("orderDate") );
            //2022-09-13但是前端显示2022-09-13 00:00:00,解决：service层把它转为String再放进map
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    
}
